/**
* 尊敬的用户，你好：页面 JS 面板是高阶用法，一般不建议普通用户使用，如需使用，请确定你具备研发背景，能够自我排查问题。当然，你也可以咨询身边的技术顾问或者联系宜搭平台的技术支持获得服务（可能收费）。
* 我们可以用 JS 面板来开发一些定制度高功能，比如：调用阿里云接口用来做图像识别、上报用户使用数据（如加载完成打点）等等。
* 你可以点击面板上方的 「使用帮助」了解。
*/

// 当页面渲染完毕后马上调用下面的函数，这个函数是在当前页面 - 设置 - 生命周期 - 页面加载完成时中被关联的。
export function didMount() {
  console.log(`「页面 JS」：当前页面地址 ${location.href}`);
  // console.log(`「页面 JS」：当前页面 id 参数为 ${this.state.urlParams.id}`);
  // 更多 this 相关 API 请参考：https://www.yuque.com/yida/support/ocmxyv#OCEXd
  // document.title = window.loginUser.userName + ' | 宜搭';

  // 组织
  loadOrgs(this);
  // 采购单位
  loadUnit(this);
  // 基本单位
  loadUnit(this, 1);
  // 物料存储条件
  loadStorageConditions(this);
  // 物料存货类别
  loadCategory(this);
  // 物料材料分类
  loadClass(this);
  // 物料标本类型
  loadSpecimenType(this);
  // 物料分组
  loadGroup(this);
  // 生产厂家
  loadManufacturer(this);
  // 加载仓库
  loadStock(this);
  // 加载(DH码)成本对象 - 物料信息
  loadMaterial(this);

}

/**
* TableField onAddClick
*/
export function onAddMaterial() {
  // 组织
  loadOrgs(this);
  // 采购单位
  loadUnit(this);
  // 基本单位
  loadUnit(this, 1);
  // 物料存储条件
  loadStorageConditions(this);
  // 物料存货类别
  loadCategory(this);
  // 物料材料分类
  loadClass(this);
  // 物料标本类型
  loadSpecimenType(this);
  // 物料分组
  loadGroup(this);
  // 生产厂家
  loadManufacturer(this);
  // 加载仓库
  loadStock(this);
  // 加载(DH码)成本对象 - 物料信息
  loadMaterial(this);

}

/**
 * 加载云星空组织信息
 */
function loadOrgs(element) {
  let orgParams = {};
  let orgData = element.dataSourceMap['getOrgs'].load(orgParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.orgName,
        text: item.orgName,
        value: item.orgId
      }
      if (1 != item.orgId) {
        values.push(arr);
      }
    })
    element.state.datas = values;
    return values;
  });
  orgData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');

    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'multiSelectField_l4w2r4m9', { dataSource: res });
    });

  })
}

/**
 * 加载采购/基本单位
 */
function loadUnit(element, isBaseUnit) {
  let unitParams = {};
  if (isBaseUnit != '' && isBaseUnit != undefined && isBaseUnit != 'undefined') {
    unitParams = { "isBaseUnit": isBaseUnit };
  }
  let unitData = element.dataSourceMap['getMaterialUnit'].load(unitParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    Object.keys(res).forEach(function (key) {
      let item = res[key];
      let arr = {
        label: item.FName,
        text: item.FName + "(" + item.FNumber + ")",
        value: item.FNumber
      }
      values.push(arr);
    });
    element.state.datas = values;
    return values;
  });
  unitData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');

    tableField.getItems().map(list => {
      if (isBaseUnit != '' && isBaseUnit != undefined && isBaseUnit != 'undefined') {
        tableField.setComponentProps(list, 'selectField_l4w2r4ml', { dataSource: res });
      } else {
        tableField.setComponentProps(list, 'selectField_l4w2r4mc', { dataSource: res });
      }
    });

  })
}
// 存储条件
function loadStorageConditions(element) {
  let condParams = {};
  let condData = element.dataSourceMap['getMaterialStorageConditions'].load(condParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.FName,
        text: item.FName,
        value: item.FNumber
      }
      values.push(arr);
    });
    element.state.datas = values;
    return values;
  });
  condData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');
    // const data = tableField.getItems()[tableField.getItems().length - 1]
    // tableField.setComponentProps(data, 'selectField_l4w2r4m6', { dataSource: res });

    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l4w2r4m6', { dataSource: res });
    });

  })
}

/**
 * 加载物料存货类别
 */
function loadCategory(element) {
  let categoryParams = {};
  let categoryData = element.dataSourceMap['getMaterialCategory'].load(categoryParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.FName,
        text: item.FName,
        value: item.FNumber
      }
      values.push(arr);
    });
    element.state.datas = values;
    return values;
  });
  categoryData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');
    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l4w2r4mk', { dataSource: res });
    });
  })
}

/**
 * 加载物料材料分类
 */
function loadClass(element) {
  let classParams = {};
  let classData = element.dataSourceMap['getMaterialClass'].load(classParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.FDataValue,
        text: item.FDataValue,
        value: item.FNumber
      }
      values.push(arr);
    });
    element.state.datas = values;
    return values;
  });
  classData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');
    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l4w74qs1', { dataSource: res });
    });

  })
}

/**
 * 加载物料标本类型
 */
function loadSpecimenType(element) {
  let typeParams = {};
  let typeData = element.dataSourceMap['getMaterialSpecimenType'].load(typeParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.FDataValue,
        text: item.FDataValue,
        value: item.FNumber
      }
      values.push(arr);
    });
    element.state.datas = values;
    return values;
  });
  typeData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');
    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l4w2r4mm', { dataSource: res });
    });

  })
}

/**
 * 加载物料分组
 */
function loadGroup(element) {
  let groupParams = {};
  let groupData = element.dataSourceMap['getMaterialGroup'].load(groupParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {};
      if (item.FParentName != undefined) {
        arr = {
          label: item.FName,
          text: item.FParentName + "(" + item.FParentNumber + ")-" + item.FName + "(" + item.FNumber + ")",
          value: item.FNumber
        }
      } else {
        arr = {
          label: item.FName,
          text: item.FName + "(" + item.FNumber + ")",
          value: item.FNumber
        }
      }
      values.push(arr);
    });
    element.state.datas = values;
    return values;
  });
  groupData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');
    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l4w2r4mj', { dataSource: res });
    });

  })
}

/**
 * 加载物料生产厂家
 */
function loadManufacturer(element) {
  let manufacturerParams = {};
  let manufacturerData = element.dataSourceMap['getMaterialManufacturer'].load(manufacturerParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.FName,
        text: item.FName,
        value: item.FNumber
      }
      values.push(arr);
    })
    element.state.datas = values;
    return values;
  });
  manufacturerData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');

    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l526r1gu', { dataSource: res });
    });

  })
}

/**
 * 加载仓库信息
 */
function loadStock(element) {
  let stockParams = { "stockProperty": "1", "stockOrgId": "100049", "group": "1058132" };
  let stockData = element.dataSourceMap['getStock'].load(stockParams).then(res => {
    const values = [];
    //console.log(JSON.stringify(res));
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.FName,
        text: item.FName,
        value: item.FNumber
      }
      values.push(arr);
    })
    element.state.datas = values;
    return values;
  });
  stockData.then(res => {
    const tableField = element.$('tableField_ktp7s33s');
    tableField.getItems().map(list => {
      tableField.setComponentProps(list, 'selectField_l4w2r4mg', { dataSource: res });
    });
  })
}

/**
 * 加载云星空物料信息
 */
function loadMaterial(element) {

  const tableField = element.$('tableField_ktp7s33s');
  tableField.getItems().map(list => {

    let number = tableField.getComponent(list, "selectField_l5tcraty").getValue();

    if (number == undefined || number == "undefined") {
      number = "";
    }

    let materialParams = { "keyWord": number, "limit": 500, "erpClsId": "2" };
    let materialData = element.dataSourceMap['getMaterials'].load(materialParams).then(res => {
      const values = [];
      if (res == null || res.length == 0) return;
      res.map(item => {
        let arr = {
          label: item.fname,
          text: item.fnumber + " " + item.fname,
          value: item.fnumber
        }
        values.push(arr);
      })
      element.state.datas = values;
      return values;
    });

    materialData.then(res => {
      tableField.setComponentProps(list, 'selectField_l5tcraty', { dataSource: res });
    });
  });
}


/**
 * 校验excel导入的物料
 */
export async function onActionClick({ index, groupId, itemValue, actionKey }) {
  this.utils.dialog({
    type: 'notice',
    title: '友情提示',
    content: <html>1. 执行校验大概需要30s-60s,请勿刷新页面或重复点击校验按钮<br />2. 校验完成后请检查如下拉框等动态加载数据是否正确<br />3. 渲染失败数据请手动更正后再提交,否则无法回写云星空</html>, // 如需换行可传入 HTML/JSX 来实现
    hasMask: true
  });

  const tab = this.$('tableField_ktp7s33s');
  console.log(tab, "tab")
  let getItemsList = tab.getItems()
  for (let i = 0; i < getItemsList.length; i++) {
    let list = getItemsList[i]
    // tab.getItems().map(async list => {
    // console.log("item:" + JSON.stringify(list));
    // 组织
    await loadOrgs(this);
    // 加载仓库
    await loadStock(this);
    // 物料材料分类
    await loadClass(this);
    // 物料标本类型
    await loadSpecimenType(this);
    // 组织
    const org = tab.getComponent(list, "multiSelectField_l4w2r4m9").getValue();
    if (org == null || org == '' || org == undefined || org == 'undefined') {
      tab.getComponent(list, "multiSelectField_l4w2r4m9").setValue();
    }
    // DH码
    let number = tab.getComponent(list, "selectField_l5tcraty").getValue();
    console.log(number, "number")
    if (number == null || number == 'null' || number == '' || number == undefined || number == "undefined") number = "";
    let materialParams = {
      "keyWord": number,
      "limit": 500,
      "erpClsId": "2"
    };
    this.dataSourceMap['getMaterials'].load(materialParams).then(res => {
      const values = [];
      if (res == null || res.length == 0) return;
      res.map(item => {
        let arr = {
          label: item.fname,
          text: item.fnumber + " " + item.fname,
          value: item.fnumber
        }
        values.push(arr);
      })
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l5tcraty', {
        dataSource: res
      });
      if (res == undefined || res == 'undefined' || res.length == 0) {
        tab.getComponent(list, "selectField_l5tcraty").setValue();
      }
    });

    // 基本单位
    let unitParams = {
      "isBaseUnit": 1
    };
    this.dataSourceMap['getMaterialUnit'].load(unitParams).then(res => {
      const values = [];
      //console.log(JSON.stringify(res));
      if (res == null || res.length == 0) return;
      Object.keys(res).forEach(function (key) {
        let item = res[key];
        let arr = {
          label: item.FName,
          text: item.FName + "(" + item.FNumber + ")",
          value: item.FNumber
        }
        values.push(arr);
      });
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l4w2r4ml', {
        dataSource: res
      });
      new Promise((resolve) => {
        let numbers = [];
        res.map(g => {
          numbers.push(g.value);
        })
        resolve(numbers);
      }).then(res => {
        let val = tab.getComponent(list, 'selectField_l4w2r4ml').getValue();
        if (res.indexOf(val) == -1) {
          tab.getComponent(list, 'selectField_l4w2r4ml').setValue();
        }
      });
    });

    // 采购单位
    this.dataSourceMap['getMaterialUnit'].load().then(res => {
      const values = [];
      //console.log(JSON.stringify(res));
      if (res == null || res.length == 0) return;
      Object.keys(res).forEach(function (key) {
        let item = res[key];
        let arr = {
          label: item.FName,
          text: item.FName + "(" + item.FNumber + ")",
          value: item.FNumber
        }
        values.push(arr);
      });
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l4w2r4mc', {
        dataSource: res
      });
      new Promise((resolve) => {
        let numbers = [];
        res.map(g => {
          numbers.push(g.value);
        })
        resolve(numbers);
      }).then(res => {
        let val = tab.getComponent(list, 'selectField_l4w2r4mc').getValue();
        if (res.indexOf(val) == -1) {
          tab.getComponent(list, 'selectField_l4w2r4mc').setValue();
        }
      });
    });

    // 生产厂家
    this.dataSourceMap['getMaterialManufacturer'].load().then(res => {
      const values = [];
      if (res == null || res.length == 0) return;
      res.map(item => {
        let arr = {
          label: item.FName,
          text: item.FName,
          value: item.FNumber
        }
        values.push(arr);
      })
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l526r1gu', {
        dataSource: res
      });
      new Promise((resolve) => {
        let numbers = [];
        res.map(g => {
          numbers.push(g.value);
        })
        resolve(numbers);
      }).then(res => {
        let val = tab.getComponent(list, 'selectField_l526r1gu').getValue();
        if (res.indexOf(val) == -1) {
          tab.getComponent(list, 'selectField_l526r1gu').setValue();
          tab.setComponentProps(list, 'selectField_l526r1gu', {
            validation: [{
              type: "required"
            }]
          });
        }
      });
    });

    // 存储条件
    this.dataSourceMap['getMaterialStorageConditions'].load().then(res => {
      const values = [];
      if (res == null || res.length == 0) return;
      res.map(item => {
        let arr = {
          label: item.FName,
          text: item.FName,
          value: item.FNumber
        }
        values.push(arr);
      });
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l4w2r4m6', {
        dataSource: res
      });
      new Promise((resolve) => {
        let numbers = [];
        res.map(g => {
          numbers.push(g.label);
        })
        resolve([numbers, res]);
      }).then(res => {
        let val = tab.getComponent(list, 'selectField_l4w2r4m6').getValue();
        console.log(val, "hvvvvvv")
        if (res[0].indexOf(val) == -1) {
          tab.getComponent(list, 'selectField_l4w2r4m6').setValue();
        } else {
          res[1].map(c => {
            if (c.label == val) {
              tab.getComponent(list, 'selectField_l4w2r4m6').setValue(c.value);
              console.log(tab.getComponent(list, 'selectField_l4w2r4m6').getValue(), 'piiiiii');
            }
          });
        }
      });
    });


    // 物料分组
    this.dataSourceMap['getMaterialGroup'].load().then(res => {
      const values = [];
      if (res == null || res.length == 0) return;
      res.map(item => {
        let arr = {};
        if (item.FParentName != undefined) {
          arr = {
            label: item.FName,
            text: item.FParentName + "(" + item.FParentNumber + ")-" + item.FName + "(" + item.FNumber + ")",
            value: item.FNumber
          }
        } else {
          arr = {
            label: item.FName,
            text: item.FName + "(" + item.FNumber + ")",
            value: item.FNumber
          }
        }
        values.push(arr);
      });
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l4w2r4mj', {
        dataSource: res
      });
      new Promise((resolve) => {
        let numbers = [];
        res.map(g => {
          numbers.push(g.value);
        })
        resolve(numbers);
      }).then(res => {
        let val = tab.getComponent(list, 'selectField_l4w2r4mj').getValue();
        if (res.indexOf(val) == -1) {
          tab.getComponent(list, 'selectField_l4w2r4mj').setValue();
        }
      });
    });

    // 存货类别
    this.dataSourceMap['getMaterialCategory'].load().then(res => {
      const values = [];
      if (res == null || res.length == 0) return;
      res.map(item => {
        let arr = {
          label: item.FName,
          text: item.FName,
          value: item.FNumber
        }
        values.push(arr);
      });
      return values;
    }).then(res => {
      tab.setComponentProps(list, 'selectField_l4w2r4mk', {
        dataSource: res
      });

      new Promise((resolve) => {
        let numbers = [];
        res.map(g => {
          numbers.push(g.label);
        })
        resolve([numbers, res]);
      }).then(res => {
        let val = tab.getComponent(list, 'selectField_l4w2r4mk').getValue();
        if (res[0].indexOf(val) == -1) {
          tab.getComponent(list, 'selectField_l4w2r4mk').setValue();
        } else {
          res[1].map(c => {
            if (c.label == val) {
              tab.getComponent(list, 'selectField_l4w2r4mk').setValue(val);
              return;
            }
          });
        }
      });
    });



    // 保质期
    let qybzq = tab.getComponent(list, 'radioField_l1uggwnb').getValue();
    if (qybzq == null || qybzq == '' || qybzq == undefined || qybzq == 'undefined' || qybzq == 'false' || qybzq == false) {
      tab.setComponentProps(list, 'selectField_l4w2r4mi', {
        validation: [{
          message: ""
        }]
      });
      tab.setComponentProps(list, 'numberField_l6q0kbgf', {
        validation: []
      });
      tab.setComponentProps(list, 'textField_l1uggwnc', {
        validation: []
      });
    } else {
      tab.setComponentProps(list, 'selectField_l4w2r4mi', {
        validation: [{
          type: "required"
        }]
      });
      tab.setComponentProps(list, 'numberField_l6q0kbgf', {
        validation: [{
          type: "required"
        }]
      });
      tab.setComponentProps(list, 'textField_l1uggwnc', {
        validation: [{
          type: "required"
        }]
      });
    }
    // 保质期单位
    let bzqdw = tab.getComponent(list, 'selectField_l4w2r4mi').getValue();
    if (bzqdw == '日') {
      tab.getComponent(list, 'selectField_l4w2r4mi').setValue('D');
    } else if (bzqdw == '月') {
      tab.getComponent(list, 'selectField_l4w2r4mi').setValue('M');
    } else if (bzqdw == '年') {
      tab.getComponent(list, 'selectField_l4w2r4mi').setValue('Y');
    } else {
      tab.getComponent(list, 'selectField_l4w2r4mi').setValue('');
    }
  }
}

// function loadData(element) {
//   // 组织
//   loadOrgs(element);
//   // 采购单位
//   loadUnit(element);
//   // 基本单位
//   loadUnit(element, 1);
//   // 物料存储条件
//   loadStorageConditions(element);
//   // 物料存货类别
//   loadCategory(element);
//   // 物料材料分类
//   loadClass(element);
//   // 物料标本类型
//   loadSpecimenType(element);
//   // 物料分组
//   loadGroup(element);
//   // 生产厂家
//   loadManufacturer(element);
//   // 加载仓库
//   loadStock(element);
//   // 加载(DH码)成本对象 - 物料信息
//   loadMaterial(element);
// } 
/**
* TableField beforeImportData
*/
export function beforeImportData(data) {
  console.log(JSON.stringify(data), "beforeImportData");

  data.map(item => {
    const orgCode = item.multiSelectField_l4w2r4m9;
    // 使用组织
    new Promise((resolve, reject) => {
      let orgIds = [];
      orgCode.map(code => {
        let orgParams = { "orgCode": code };
        let orgData = this.dataSourceMap['getOrgs'].load(orgParams).then(res => {
          if (res == null || res.length == 0) return;
          orgIds.push(res[0].orgId);
        });
      });
      return resolve(orgIds);
    }).then(res => {
      item.multiSelectField_l4w2r4m9 = res;
    });
  });
  return data;
}

export function beforeSubmit({ formDataMap }) {
  // 注意：目前不支持在这里修改提交数据
  console.log('beforeSubmit:', JSON.stringify(formDataMap));

  const tab = this.$('tableField_ktp7s33s');
  tab.getItems().map(list => {
    // 组织
    const org = tab.getComponent(list, "multiSelectField_l4w2r4m9").getValue();
    if (org == null || org == '' || org == undefined || org == 'undefined') {
      tab.getComponent(list, "multiSelectField_l4w2r4m9").setValue();
    }

    // 基本单位
    const baseUnit = tab.getComponent(list, 'selectField_l4w2r4ml').getValue();
    if (baseUnit == null || baseUnit == '' || baseUnit == undefined || baseUnit == 'undefined') {
      tab.getComponent(list, "selectField_l4w2r4ml").setValue();
    }

    // 采购单位
    const cgUnit = tab.getComponent(list, 'selectField_l4w2r4mc').getValue();
    if (cgUnit == null || cgUnit == '' || cgUnit == undefined || cgUnit == 'undefined') {
      tab.getComponent(list, "selectField_l4w2r4mc").setValue();
    }

    // 生产厂家
    const sccj = tab.getComponent(list, 'selectField_l526r1gu').getValue();
    if (sccj == null || sccj == '' || sccj == undefined || sccj == 'undefined') {
      tab.getComponent(list, "selectField_l526r1gu").setValue();
    }

    // 存储条件
    const cctj = tab.getComponent(list, 'selectField_l4w2r4m6').getValue();
    console.log(cctj, "tj")
    if (cctj == null || cctj == '' || cctj == undefined || cctj == 'undefined') {
      tab.getComponent(list, "selectField_l4w2r4m6").setValue();
    }

    // 物料分组
    const wlfz = tab.getComponent(list, 'selectField_l4w2r4mj').getValue();
    if (wlfz == null || wlfz == '' || wlfz == undefined || wlfz == 'undefined') {
      tab.getComponent(list, "selectField_l4w2r4mj").setValue();
    }

    // 存货类别
    const chlb = tab.getComponent(list, 'selectField_l4w2r4mk').getValue();
    if (chlb == null || chlb == '' || chlb == undefined || chlb == 'undefined') {
      tab.getComponent(list, "selectField_l4w2r4mk").setValue();
    }

    // 物料材料分类
    // 物料标本类型

    // 保质期
    let qybzq = tab.getComponent(list, 'radioField_l1uggwnb').getValue();
    if (qybzq == null || qybzq == '' || qybzq == undefined || qybzq == 'undefined' || qybzq == 'false' || qybzq == false) {
      tab.setComponentProps(list, 'selectField_l4w2r4mi', { validation: [{ message: "" }] });
      tab.setComponentProps(list, 'numberField_l6q0kbgf', { validation: [] });
      tab.setComponentProps(list, 'textField_l1uggwnc', { validation: [] });
    } else {
      tab.setComponentProps(list, 'selectField_l4w2r4mi', { validation: [{ type: "required" }] });
      tab.setComponentProps(list, 'numberField_l6q0kbgf', { validation: [{ type: "required" }] });
      tab.setComponentProps(list, 'textField_l1uggwnc', { validation: [{ type: "required" }] });
    }
  });

  const table = formDataMap['tableField_ktp7s33s'].fieldData.value;
  // 需要时可返回 false 阻止提交，支持 Promise
  return new Promise((resolve) => {

    table.map(tab => {
      tab.map(el => {
        console.log(el, "提交")
        // 组织
        if (el.fieldId == 'multiSelectField_l4w2r4m9') {
          const val = el.fieldData.value;
          const text = el.fieldData.text;
          if (val == null || val == '' || val == undefined || val == 'undefined' || text == null || text == '' || text == undefined || text == 'undefined') {
            toast(this, '使用组织为必填项，请核查！！！');
            resolve(false);
          }
        }
        // 基本单位
        if (el.fieldId == 'selectField_l4w2r4ml') {
          const val = el.fieldData.value;
          const text = el.fieldData.text;
          console.log(el, "基本单位")
          if (val == null || val == '' || val == undefined || val == 'undefined' || text == null || text == '' || text == undefined || text == 'undefined') {
            toast(this, '基本单位为必填项，请核查！！！');
            resolve(false);
          }
        }
        // 采购单位
        if (el.fieldId == 'selectField_l4w2r4mc') {
          const val = el.fieldData.value;
          const text = el.fieldData.text;
          if (val == null || val == '' || val == undefined || val == 'undefined' || text == null || text == '' || text == undefined || text == 'undefined') {
            toast(this, '采购单位为必填项，请核查！！！');
            resolve(false);
          }
        }
        // 生产厂家
        if (el.fieldId == 'selectField_l526r1gu') {
          const val = el.fieldData.value;
          const text = el.fieldData.text;
          console.log(el, "生成厂家")
          if (val == null || val == '' || val == undefined || val == 'undefined' || text == null || text == '' || text == undefined || text == 'undefined') {
            toast(this, '生产厂家必填项，请核查！！！');
            resolve(false);
          }
        }
        // 存储条件
        if (el.fieldId == 'selectField_l4w2r4m6') {
          const val = el.fieldData.value;
          const text = el.fieldData.text;
          console.log(el.fieldData, el, "ffffffffffff")
          console.log("111:" + val + " | " + text);
          if (val == null || val == '' || val == undefined || val == 'undefined' || text == null || text == '' || text == undefined || text == 'undefined') {
            toast(this, '存储条件必填项，请核查！！！');
            resolve(false);
          }
        }

        // 物料分组
        if (el.fieldId == 'selectField_l4w2r4mj') {
          const val = el.fieldData.value;
          const text = el.fieldData.text;

          if (val == null || val == '' || val == undefined || val == 'undefined' || text == null || text == '' || text == undefined || text == 'undefined') {
            toast(this, '物料分组必填项，请核查！！！');
            resolve(false);
          }
        }

        // 存货类别
        if (el.fieldId == 'selectField_l4w2r4mk') {
          const val = el.fieldData.value;
          //const text = el.fieldData.text;   || text == null || text == '' || text == undefined || text == 'undefined'
          if (val == null || val == '' || val == undefined || val == 'undefined') {
            console.log(val + " | " + text);
            toast(this, '存货类别必填项，请核查！！！');
            resolve(false);
          }
        }
      })
    });
    resolve(true);
  });
}

/**
* selectField onChange
* @param value: {mixed} 选中的值
* @param actionType: {String} 触发的方式, 'itemClick', 'enter', 'change'
* @param item: {mixed} 选中的值的对象数据
*/
export function onChange({ value, actionType, item }) {
  const tab = this.$('tableField_ktp7s33s');
  tab.getItems().map(list => {
    if (tab.getComponent(list, 'selectField_l4w2r4ml').getValue() == value) {
      let purchaseUnit = tab.getComponent(list, "selectField_l4w2r4mc").getValue();
      if (purchaseUnit == null || purchaseUnit == undefined || purchaseUnit == 'undefined') {
        tab.getComponent(list, "selectField_l4w2r4mc").setValue(value);
      }
    }
  });
}

/**
* selectField onSearch
* @param value: {String} 数据
*/
export function onSearchMaterial(keyword, item) {
  //console.log('onSearch', keyword);

  let materialParams = { "keyWord": keyword, "limit": 500, "erpClsId": "2" };
  let materialData = this.dataSourceMap['getMaterials'].load(materialParams).then(res => {
    const values = [];
    if (res == null || res.length == 0) return;
    res.map(item => {
      let arr = {
        label: item.fname,
        text: item.fnumber + " " + item.fname,
        value: item.fnumber
      }
      values.push(arr);
    })
    this.state.datas = values;
    return values;
  });

  materialData.then(res => {
    const tableField = this.$('tableField_ktp7s33s');
    tableField.setComponentProps(item.formGroupId, 'selectField_l5tcraty', { dataSource: res });
  });

  // 注：需开启“可搜索”
  // 如果 Select 需要调用远程接口搜索，请将下方的 selectOptions 改为实际的数据源名称
  // return this.dataSourceMap['selectOptions'].load({keyword: keyword});
}

/**
* selectField onChange
* @param value: {mixed} 选中的值
* @param actionType: {String} 触发的方式, 'itemClick', 'enter', 'change'
* @param item: {mixed} 选中的值的对象数据
*/
export function onMaterialCategoryChange({ value, actionType, item }) {
  console.log('onChange', value);

  if ("CHLB_009" == value) {
    // DH码必填
    const tab = this.$('tableField_ktp7s33s');
    tab.getItems().map(list => {
      if (tab.getComponent(list, 'selectField_l4w2r4mk').getValue() == value) {
        tab.setComponentProps(list, 'selectField_l5tcraty', { validation: [{ type: "required" }] });
      }
    });
  } else {
    // DH码取消必填
    const tab = this.$('tableField_ktp7s33s');
    tab.getItems().map(list => {
      if (tab.getComponent(list, 'selectField_l4w2r4mk').getValue() == value) {
        tab.setComponentProps(list, 'selectField_l5tcraty', { validation: '' });
      }
    });
  }
}
/**
* radioField onChange
* @param value 被选中的选项的值
*/
export function onChange2({ value }) {
  console.log('onChange', value);

  const tab = this.$('tableField_ktp7s33s');

  tab.getItems().map(list => {
    if (value == null || value == '' || value == undefined || value == 'undefined' || value == 'false' || value == false) {
      if (tab.getComponent(list, 'radioField_l1uggwnb').getValue() == value) {
        tab.setComponentProps(list, 'selectField_l4w2r4mi', { validation: [{ message: "" }] });
        tab.setComponentProps(list, 'numberField_l6q0kbgf', { validation: [] });
        tab.setComponentProps(list, 'textField_l1uggwnc', { validation: [] });
      }
    } else {
      if (tab.getComponent(list, 'radioField_l1uggwnb').getValue() == value) {
        tab.setComponentProps(list, 'selectField_l4w2r4mi', { validation: [{ type: "required" }] });
        tab.setComponentProps(list, 'numberField_l6q0kbgf', { validation: [{ type: "required" }] });
        tab.setComponentProps(list, 'textField_l1uggwnc', { validation: [{ type: "required" }] });
      }
    }
  });
}

/**
* textField onChange
* @param value 当前值
*/
export function onChange3({ value }) {
  console.log('onChange', value);

  const tab = this.$('tableField_ktp7s33s');

  tab.getItems().map(list => {
    let val1 = tab.getComponent(list, 'textField_l1uggwmz').getValue();
    let val2 = tab.getComponent(list, 'textField_l1uggwmy').getValue();
    let val3 = tab.getComponent(list, 'textField_l4w2r4md').getValue();

    let flag1 = val1 == null || val1 == '' || val1 == undefined || val1 == 'undefined';
    let flag2 = val2 == null || val2 == '' || val2 == undefined || val2 == 'undefined';
    let flag3 = val3 == null || val3 == '' || val3 == undefined || val3 == 'undefined';

    if (flag1 && flag2 && flag3) {
      tab.setComponentProps(list, 'textField_l1uggwmz', { validation: [] });
      tab.setComponentProps(list, 'textField_l1uggwmy', { validation: [] });
      tab.setComponentProps(list, 'textField_l4w2r4md', { validation: [] });
    } else {
      tab.setComponentProps(list, 'textField_l1uggwmz', { validation: [{ type: "required" }] });
      tab.setComponentProps(list, 'textField_l1uggwmy', { validation: [{ type: "required" }] });
      tab.setComponentProps(list, 'textField_l4w2r4md', { validation: [{ type: "required" }] });
    }
  });
}

/**
* numberField onChange
* @param value 当前值
*/
export function onChange4({ value }) {
  console.log('onChange', value);

  const tab = this.$('tableField_ktp7s33s');
  tab.getItems().map(list => {
    if (tab.getComponent(list, 'numberField_l6q0kbgf').getValue() == value) {
      tab.getComponent(list, 'textField_l1uggwnc').setValue(value);
    }
  });
}
function toast(element, content) {
  element.utils.toast({
    title: '提示信息', // 'success', 'warning', 'error', 'notice', 'help', 'loading'
    type: 'error',
    size: 'large',
    content: content,
    duration: 3000, // 毫秒, type 为 loding 时无效
  });
}

/**
* selectField onChange
* @param value: {mixed} 选中的值
* @param actionType: {String} 触发的方式, 'itemClick', 'enter', 'change'
* @param item: {mixed} 选中的值的对象数据
*/
export function onChange5({ value, actionType, item }) {
  console.log('onChange', value, item);
}